"use strict";

exports.__esModule = true;
exports.default = toggleMergeItem;
var C = _interopRequireWildcard(require("../../../i18n/constants"));
var _cellCoords = _interopRequireDefault(require("../cellCoords"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/**
 * @param {*} plugin The plugin instance.
 * @returns {object}
 */
function toggleMergeItem(plugin) {
  return {
    key: 'mergeCells',
    name() {
      const selection = this.getSelectedActive();
      if (selection) {
        const info = plugin.mergedCellsCollection.get(selection[0], selection[1]);
        if (info.row === selection[0] && info.col === selection[1] && info.row + info.rowspan - 1 === selection[2] && info.col + info.colspan - 1 === selection[3]) {
          return this.getTranslatedPhrase(C.CONTEXTMENU_ITEMS_UNMERGE_CELLS);
        }
      }
      return this.getTranslatedPhrase(C.CONTEXTMENU_ITEMS_MERGE_CELLS);
    },
    callback() {
      const activeRange = this.getSelectedRangeActive();
      if (!activeRange) {
        return;
      }
      activeRange.setDirection(this.isRtl() ? 'NE-SW' : 'NW-SE');
      const {
        from,
        to
      } = activeRange;
      plugin.toggleMerge(activeRange);
      this.selectCell(from.row, from.col, to.row, to.col, false);
    },
    disabled() {
      const selection = this.getSelectedActive();
      if (!selection) {
        return true;
      }
      const isSingleCell = _cellCoords.default.isSingleCell({
        row: selection[0],
        col: selection[1],
        rowspan: selection[2] - selection[0] + 1,
        colspan: selection[3] - selection[1] + 1
      });
      return isSingleCell || this.selection.isSelectedByCorner();
    },
    hidden: false
  };
}